<template>
  <div>
    <el-button @click="addAdmin" type="primary">添加频道管理员</el-button>
    <el-dialog title="添加频道管理员" :visible.sync="dialogVisible" width="30%" :modal="false" :before-close="beforeClose" :close-on-click-modal="false">
      <el-form :model="adminform" ref="adminform" class="txtC">
        <div>
          <span class="w90">邮箱:</span>
          <el-input v-model="adminform.email" class="w300" :disabled="!dialogModify" maxlength="50"></el-input>
        </div>
        <div>
          <span class="w90 mgt20">密码:</span>
          <el-input v-model="adminform.pwd" type="password" maxlength="16" class="w300"></el-input>
        </div>
        <div>
          <span class="w90 mgt20">用户名:</span>
          <el-input v-model="adminform.userName" class="w300" maxlength="20" placeholder="最长20个字符"></el-input>
        </div>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="saveManager()" :loading="isSaving" type="primary">保存</el-button>
      </span>
    </el-dialog>
    <hr>
    <xy-table :tableData="tableData" :loading="tableData.isLoading" @change="getManaList" v-show="!tableData.firtIn" :noEmpty="tableData.list.length==0?false:true">
      <el-table-column label="账号邮箱" prop="email" align="center"></el-table-column>
      <el-table-column label="密码" prop="outPasswd" align="center"></el-table-column>
      <el-table-column label="用户名" prop="name" align="center"></el-table-column>
      <el-table-column label="操作" align="center" min-width="160px">
        <template slot-scope="scope">
          <el-button @click="chanLink(scope.row)">频道关联</el-button>
          <el-button @click="modify(scope.row)">修改</el-button>
          <el-button @click="freeze(scope)" v-show="scope.row.isActive">冻结</el-button>
          <el-button @click="active(scope)" v-show="!scope.row.isActive">激活</el-button>
        </template>
      </el-table-column>
    </xy-table>
    <el-dialog title="频道关联" :visible.sync="dialogTableVisible">
      <el-button @click="addChannel">添加频道号</el-button>
      <xy-table :tableData="associationList" :loading="associationList.isLoading" @change="getManaChanList" :noEmpty="associationList.list.length==0?false:true">
        <el-table-column property="name" label="频道名称" min-width="150" align="center"></el-table-column>
        <el-table-column property="channelId" label="频道号" min-width="150" align="center"></el-table-column>
        <el-table-column label="操作" min-width="150" align="center">
          <template slot-scope="scope">
            <el-button type="text" @click="devideLink(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </xy-table>

      <el-dialog width="50%" title="增删频道" :visible.sync="innerVisible" append-to-body>
        <xy-table :tableData="channelList" :loading="channelList.isLoading" @change="getAllChan" v-show="!channelList.firtIn" :noEmpty="channelList.list.length==0?false:true">
          <el-table-column property="name" label="频道名称" min-width="150" align="center"></el-table-column>
          <el-table-column property="channelId" label="频道号" min-width="150" align="center"></el-table-column>
          <el-table-column label="操作" min-width="150" align="center">
            <template slot-scope="scope">
              <el-button type="text" @click="linkChan(scope.row)" v-show="!scope.row.isLinked" :loading="scope.row.isLoading">添加</el-button>
              <el-button type="text" v-show="scope.row.isLinked" disabled>已添加</el-button>
            </template>
          </el-table-column>
        </xy-table>
      </el-dialog>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      allChanCurPage:'',
      This:'',
      addManaAjax:'fsdf',
      beforeModifData:null,
      curChanManaEmail:'',
      diaTitle: '',
      hasAppMana: false,
      isSaving: false,
      devAppId: '',
      channelList: {
        firtIn:true,
        isLoading:false,
        total: 0,
        page: 1,
        size: 8,
        list: []
      },
      associationList: {
        page:1,
        size:10,
        total: 0,
        list: [],
        isLoading: false,
      },
      tableData: {
        firtIn:true,
        total: 0,
        list: [],
        isLoading: false,
        page: 1,
        size: 10
      },
      dialogVisible: false,
      dialogTableVisible: false,
      dialogModify: false,
      innerVisible: false,
      adminform: {
        email: "",
        pwd: "",
        userName: "",
        userType: 1
      },
      modifyform: {
        email: "",
        pwd: "",
        userName: ""
      }
    };
  },
  methods: {
    beforeClose(){
      if(!this.adminform.email && !this.adminform.pwd && !this.adminform.userName){
        this.dialogVisible = false
        this.isSaving = false
      }else{
        this.$confirm('数据尚未保存，是否关闭','提示',{
          confirmButtonText:'关闭',
          cancelButtonText:'取消',
          type:'warning'
        }).then(()=>{
          this.isSaving = false
          this.dialogVisible = false
          this.adminform.email = ''
          this.adminform.pwd = ''
          this.adminform.userName = ''
        }).catch(()=>{})
      } 
    },
    modify(row) {
      this.beforeModifData = row
      this.diaTitle = "修改管理员";
      this.dialogModify = false;
      this.dialogVisible = true;
      this.adminform.email = row.email;
      this.adminform.pwd = "666666";
      this.adminform.userName = row.name;
    },
    saveModify() {
      if (!this.$utils.identifyPwd(this.beforeModifData.pwd)) {
        this.$message.error('密码长度为5~16，英文数字下划线')
        return
      }
      this.isSaving = true
      this.$postStream("/admin/instance/ccb/polyv/adminUser/update.do", {
        data: {
          email:this.beforeModifData.email,
          name:this.adminform.userName,
          passwd:this.adminform.pwd,
          appId:this.devAppId
        },
        success: res => {
          if (res.success) {
            this.$message.success('修改成功')
            this.getManaList()
            this.dialogVisible = false
            this.isSaving = false
          } else {
            this.$message.error(res.message)
            this.isSaving = false
          }
        },
        error: (res) => {
          this.$message.error(res.message)
          this.isSaving = false
        }
      });
    },
    //获取所有频道（包括已关联和未关联）
    addChannel() {
      this.innerVisible = true
      this.channelList.firtIn = false
      this.getAllChan()
    },
    getAllChan(page){
      this.allChanCurPage = page
      this.channelList.isLoading = true
      this.$postStream("/admin/instance/ccb/polyv/channel/samplelist.do", {
        data: {
          appId:this.devAppId,
          email:this.curChanManaEmail,
          page:page || this.channelList.page,
          displayRecord:this.channelList.size
        },
        success: res => {
          if (res.success) {
            this.channelList.page = page || this.channelList.page
            this.channelList.list = res.object.list
            this.channelList.total = res.object.count
            this.channelList.isLoading = false
            if(!this.channelList){}
            else{
              if(this.channelList.list.length>0){
                for(let i=0;i<this.channelList.list.length;i++){
                  this.channelList.list[i].isLoading = false
                }
              }
            }
          } else {
            this.$message.error('获取列表失败');
            this.channelList.isLoading = false
          }
        },
        error: () => {
          this.$message.error('获取列表失败');
          this.channelList.isLoading = false
        }
      });
    },
    //增加关联频道
    linkChan(row) {
      row.isLoading = true
      this.$postStream('/admin/instance/ccb/polyv/adminUser/channel/linkChannelAdmin.do',{
        data:{
          email:this.curChanManaEmail,
          appId:this.devAppId,
          modifyType:1,//1代表增加
          channelId:row.channelId
        },
        success:(res)=>{
          if(res.success){
            row.isLoading = false
            this.$message.success('成功关联频道')
            this.innerVisible = false
            if(res.object.isLinkedBefore){
              this.$message.warning('已在其他频道管理员配置')
            }
            this.getManaChanList()
          }else{
            row.isLoading = false
            if(res.code == 50001){
              this.$message.error(res.message)
            }else{
              this.$message.error('关联频道失败')
            }
          }
        },
        error:()=>{
          row.isLoading = false
          this.$message.error('关联频道失败')
        }
      })
    },
    //删除关联频道
    devideLink(row) {
      this.$postStream('/admin/instance/ccb/polyv/adminUser/channel/linkChannelAdmin.do',{
        data:{
          email:this.curChanManaEmail,
          appId:this.devAppId,
          modifyType:2,//2代表删除
          channelId:row.channelId
        },
        success:(res)=>{
          if(res.success){
            this.$message.success('成功删除频道')
            this.getManaChanList()
            this.getAllChan(this.allChanCurPage)
          }else{
            if(res.code == 50001){
              this.$message.error(res.message)
            }else{
              this.$message.error('删除频道失败')
            }
          }
        },
        error:()=>{
          this.$message.error('删除频道失败')
        }
      })
    },
    clearrest() {
      this.adminform.email = ''
      this.adminform.pwd = ''
      this.adminform.userName = ''
      this.adminform.userType = 1
    },
    addAdmin() {
      this.diaTitle = '添加管理员'
      this.dialogModify = true
      this.dialogVisible = true
      this.clearrest()
    },
    chanLink(row) {
      this.curChanManaEmail = row.email
      this.dialogTableVisible = true;
      this.getManaChanList()
    },
    //获取已经关联某个频道管理员的所有频道
    getManaChanList(page){
      this.associationList.isLoading = true
      this.associationList.firtIn = false
      this.$postStream('/admin/instance/ccb/polyv/adminUser/channel/linkChannelAdmin.do',{
        data:{
          email:this.curChanManaEmail,
          appId:this.devAppId,
          modifyType:3,//3代表获取
          page:page ||　this.associationList.page,
          displayRecord:this.associationList.size
        },
        success:(res)=>{
          if(res.success){
            this.associationList.page = page ||　this.associationList.page
            this.associationList.list = res.object.list
            this.associationList.total = res.object.count
            this.associationList.isLoading = false
          }else{
            this.associationList.isLoading = false
          }
        },
        error:()=>{
          this.associationList.isLoading = false
        }
      })
    },
    freeze(scope) {
      this.$confirm("是否冻结此账号？", {
        confirmButtonText: "确定",
        cancelButtonText: "取消"
      }).then(() => {
          this.$postStream(
            "/admin/instance/ccb/polyv/adminUser/channel/delete.do",
            {
              data: {
                email: scope.row.email,
                appId: this.devAppId
              },
              success: res => {
                if (res.success) {
                  this.getManaList();
                  this.$message.success('冻结成功')
                } else {
                  if(res.code == 50001){
                    this.$message.error(res.message)
                  }else{
                    this.$message.error('冻结失败')
                  }
                }
              },
              error: () => {
                this.$message.error('冻结失败')
              }
            }
          );
      });
    },
    active(scope){
      this.$confirm("是否激活此账号？", {
        confirmButtonText: "确定",
        cancelButtonText: "取消"
      }).then(() => {
          this.$postStream(
            "/admin/instance/ccb/polyv/adminUser/channel/active.do",
            {
              data: {
                email: scope.row.email,
                appId: this.devAppId
              },
              success: res => {
                if (res.success) {
                  this.getManaList();
                  this.$message.success('激活成功')
                } else {
                  if(res.code == 50001){
                    this.$message.error(res.message)
                  }else{
                    this.$message.error('激活失败')
                  }
                }
              },
              error: () => {
                this.$message.error('激活失败')
              }
            }
          );
      });
    },
    //添加管理员
    saveManager() {
      if (!this.adminform.email) {
        this.$message.warning("请输入邮箱");
        return;
      } else if (!this.$utils.isEmail(this.adminform.email)) {
        this.$message.warning("请填写正确邮箱格式");
        return;
      }
      if (!this.adminform.pwd) {
        this.$message.warning("请输入密码");
        return;
      } else if (!this.$utils.identifyPwd(this.adminform.pwd)) {
        this.$message({
          message: "密码长度为5~16，英文数字下划线",
          type: "warning"
        });
        return;
      }
      if (!this.adminform.userName || !(this.adminform.userName.trim())) {
        this.$message.warning("请输入用户名");
        return;
      }
      if (this.dialogModify) {
        this.addManager()
      } else {
        this.saveModify()
      }
    },
    addManager() {
      this.isSaving = true;
      this.addManaAjax = this.$postStream("/admin/instance/ccb/polyv/adminUser/save.do", {
        data: {
          appId: this.devAppId,
          email: this.adminform.email,
          name: this.adminform.userName,
          passwd: this.adminform.pwd,
          type: 2//2代表频道管理员
        },
        success: res => {
          if (res.success) {
            this.isSaving = false;
            this.clearrest();
            this.$message.success("添加成功");
            this.getManaList();
            this.dialogVisible = false;
          } else {
            this.$message.error(res.message);
            this.isSaving = false;
          }
        },
        error: res => {
          this.$message.error(res.message);
          this.isSaving = false;
        }
      });
    },
    getManaList(page) {
      this.tableData.firtIn = false
      this.tableData.isLoading = true
      this.$postStream("/admin/instance/ccb/polyv/adminUser/list.do", {
        data: {
          appId:this.devAppId,
          displayRecord:this.tableData.size,
          page:page || this.tableData.page
        },
        success: res => {
          if (res.success) {
            this.tableData.page = page || this.tableData.page
            this.tableData.list = res.object.list
            if(!this.tableData.list){
              this.tableData.list = []
            }
            this.tableData.total = res.object.count
          }else{
            this.$message.error('管理员列表获取失败')
          }
        },
        error:()=>{
          this.$message.error('管理员列表获取失败')
        },
        complete:()=>{
          this.tableData.isLoading = false
        }
      });
    }
  },
  mounted() {
    this.devAppId = sessionStorage.getItem('curAccountAppId')
    this.curChanManaEmail = sessionStorage.getItem('curAccountEmail')
    this.getManaList()
  }
};
</script>
<style lang="scss" scoped>
button{
  margin: 5px 0;
}
</style>
